use ::libc;
extern "C" {
    fn grid_get_cell(_: *mut grid, _: u_int, _: u_int, _: *mut grid_cell);
    fn grid_get_line(_: *mut grid, _: u_int) -> *mut grid_line;
    fn grid_line_length(_: *mut grid, _: u_int) -> u_int;
    fn grid_in_set(_: *mut grid, _: u_int, _: u_int, _: *const libc::c_char) -> libc::c_int;
    fn memcmp(_: *const libc::c_void, _: *const libc::c_void, _: libc::c_ulong) -> libc::c_int;
}
pub type __u_char = libc::c_uchar;
pub type __u_short = libc::c_ushort;
pub type __u_int = libc::c_uint;
pub type __time_t = libc::c_long;
pub type time_t = __time_t;
pub type u_char = __u_char;
pub type u_short = __u_short;
pub type u_int = __u_int;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_cell {
    pub data: utf8_data,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: libc::c_int,
    pub bg: libc::c_int,
    pub us: libc::c_int,
    pub link: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct utf8_data {
    pub data: [u_char; 21],
    pub have: u_char,
    pub size: u_char,
    pub width: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid {
    pub flags: libc::c_int,
    pub sx: u_int,
    pub sy: u_int,
    pub hscrolled: u_int,
    pub hsize: u_int,
    pub hlimit: u_int,
    pub linedata: *mut grid_line,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_line {
    pub celldata: *mut grid_cell_entry,
    pub cellused: u_int,
    pub cellsize: u_int,
    pub extddata: *mut grid_extd_entry,
    pub extdsize: u_int,
    pub flags: libc::c_int,
    pub time: time_t,
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_extd_entry {
    pub data: utf8_char,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: libc::c_int,
    pub bg: libc::c_int,
    pub us: libc::c_int,
    pub link: u_int,
}
pub type utf8_char = u_int;
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_cell_entry {
    pub c2rust_unnamed: C2RustUnnamed,
    pub flags: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed {
    pub offset: u_int,
    pub data: C2RustUnnamed_0,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub attr: u_char,
    pub fg: u_char,
    pub bg: u_char,
    pub data: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_reader {
    pub gd: *mut grid,
    pub cx: u_int,
    pub cy: u_int,
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_start(
    mut gr: *mut grid_reader,
    mut gd: *mut grid,
    mut cx: u_int,
    mut cy: u_int,
) {
    (*gr).gd = gd;
    (*gr).cx = cx;
    (*gr).cy = cy;
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_get_cursor(
    mut gr: *mut grid_reader,
    mut cx: *mut u_int,
    mut cy: *mut u_int,
) {
    *cx = (*gr).cx;
    *cy = (*gr).cy;
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_line_length(mut gr: *mut grid_reader) -> u_int {
    return grid_line_length((*gr).gd, (*gr).cy);
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_right(
    mut gr: *mut grid_reader,
    mut wrap: libc::c_int,
    mut all: libc::c_int,
) {
    let mut px: u_int = 0;
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    if all != 0 {
        px = (*(*gr).gd).sx;
    } else {
        px = grid_reader_line_length(gr);
    }
    if wrap != 0
        && (*gr).cx >= px
        && (*gr).cy
            < ((*(*gr).gd).hsize)
                .wrapping_add((*(*gr).gd).sy)
                .wrapping_sub(1 as libc::c_int as libc::c_uint)
    {
        grid_reader_cursor_start_of_line(gr, 0 as libc::c_int);
        grid_reader_cursor_down(gr);
    } else if (*gr).cx < px {
        (*gr).cx = ((*gr).cx).wrapping_add(1);
        while (*gr).cx < px {
            grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc);
            if !(gc.flags as libc::c_int) & 0x4 as libc::c_int != 0 {
                break;
            }
            (*gr).cx = ((*gr).cx).wrapping_add(1);
        }
    }
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_left(mut gr: *mut grid_reader, mut wrap: libc::c_int) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    while (*gr).cx > 0 as libc::c_int as libc::c_uint {
        grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc);
        if !(gc.flags as libc::c_int) & 0x4 as libc::c_int != 0 {
            break;
        }
        (*gr).cx = ((*gr).cx).wrapping_sub(1);
    }
    if (*gr).cx == 0 as libc::c_int as libc::c_uint
        && (*gr).cy > 0 as libc::c_int as libc::c_uint
        && (wrap != 0
            || (*grid_get_line(
                (*gr).gd,
                ((*gr).cy).wrapping_sub(1 as libc::c_int as libc::c_uint),
            ))
            .flags
                & 0x1 as libc::c_int
                != 0)
    {
        grid_reader_cursor_up(gr);
        grid_reader_cursor_end_of_line(gr, 0 as libc::c_int, 0 as libc::c_int);
    } else if (*gr).cx > 0 as libc::c_int as libc::c_uint {
        (*gr).cx = ((*gr).cx).wrapping_sub(1);
    }
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_down(mut gr: *mut grid_reader) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    if (*gr).cy
        < ((*(*gr).gd).hsize)
            .wrapping_add((*(*gr).gd).sy)
            .wrapping_sub(1 as libc::c_int as libc::c_uint)
    {
        (*gr).cy = ((*gr).cy).wrapping_add(1);
    }
    while (*gr).cx > 0 as libc::c_int as libc::c_uint {
        grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc);
        if !(gc.flags as libc::c_int) & 0x4 as libc::c_int != 0 {
            break;
        }
        (*gr).cx = ((*gr).cx).wrapping_sub(1);
    }
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_up(mut gr: *mut grid_reader) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    if (*gr).cy > 0 as libc::c_int as libc::c_uint {
        (*gr).cy = ((*gr).cy).wrapping_sub(1);
    }
    while (*gr).cx > 0 as libc::c_int as libc::c_uint {
        grid_get_cell((*gr).gd, (*gr).cx, (*gr).cy, &mut gc);
        if !(gc.flags as libc::c_int) & 0x4 as libc::c_int != 0 {
            break;
        }
        (*gr).cx = ((*gr).cx).wrapping_sub(1);
    }
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_start_of_line(
    mut gr: *mut grid_reader,
    mut wrap: libc::c_int,
) {
    if wrap != 0 {
        while (*gr).cy > 0 as libc::c_int as libc::c_uint
            && (*grid_get_line(
                (*gr).gd,
                ((*gr).cy).wrapping_sub(1 as libc::c_int as libc::c_uint),
            ))
            .flags
                & 0x1 as libc::c_int
                != 0
        {
            (*gr).cy = ((*gr).cy).wrapping_sub(1);
        }
    }
    (*gr).cx = 0 as libc::c_int as u_int;
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_end_of_line(
    mut gr: *mut grid_reader,
    mut wrap: libc::c_int,
    mut all: libc::c_int,
) {
    let mut yy: u_int = 0;
    if wrap != 0 {
        yy = ((*(*gr).gd).hsize)
            .wrapping_add((*(*gr).gd).sy)
            .wrapping_sub(1 as libc::c_int as libc::c_uint);
        while (*gr).cy < yy && (*grid_get_line((*gr).gd, (*gr).cy)).flags & 0x1 as libc::c_int != 0
        {
            (*gr).cy = ((*gr).cy).wrapping_add(1);
        }
    }
    if all != 0 {
        (*gr).cx = (*(*gr).gd).sx;
    } else {
        (*gr).cx = grid_reader_line_length(gr);
    };
}
unsafe extern "C" fn grid_reader_handle_wrap(
    mut gr: *mut grid_reader,
    mut xx: *mut u_int,
    mut yy: *mut u_int,
) -> libc::c_int {
    while (*gr).cx > *xx {
        if (*gr).cy == *yy {
            return 0 as libc::c_int;
        }
        grid_reader_cursor_start_of_line(gr, 0 as libc::c_int);
        grid_reader_cursor_down(gr);
        if (*grid_get_line((*gr).gd, (*gr).cy)).flags & 0x1 as libc::c_int != 0 {
            *xx = ((*(*gr).gd).sx).wrapping_sub(1 as libc::c_int as libc::c_uint);
        } else {
            *xx = grid_reader_line_length(gr);
        }
    }
    return 1 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_in_set(
    mut gr: *mut grid_reader,
    mut set: *const libc::c_char,
) -> libc::c_int {
    return grid_in_set((*gr).gd, (*gr).cx, (*gr).cy, set);
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_next_word(
    mut gr: *mut grid_reader,
    mut separators: *const libc::c_char,
) {
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    let mut width: u_int = 0;
    if (*grid_get_line((*gr).gd, (*gr).cy)).flags & 0x1 as libc::c_int != 0 {
        xx = ((*(*gr).gd).sx).wrapping_sub(1 as libc::c_int as libc::c_uint);
    } else {
        xx = grid_reader_line_length(gr);
    }
    yy = ((*(*gr).gd).hsize)
        .wrapping_add((*(*gr).gd).sy)
        .wrapping_sub(1 as libc::c_int as libc::c_uint);
    if grid_reader_handle_wrap(gr, &mut xx, &mut yy) == 0 {
        return;
    }
    if grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) == 0 {
        if grid_reader_in_set(gr, separators) != 0 {
            loop {
                (*gr).cx = ((*gr).cx).wrapping_add(1);
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && grid_reader_in_set(gr, separators) != 0
                    && grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) == 0)
                {
                    break;
                }
            }
        } else {
            loop {
                (*gr).cx = ((*gr).cx).wrapping_add(1);
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && !(grid_reader_in_set(gr, separators) != 0
                        || grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char)
                            != 0))
                {
                    break;
                }
            }
        }
    }
    while grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0 && {
        width = grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) as u_int;
        width != 0
    } {
        (*gr).cx = ((*gr).cx as libc::c_uint).wrapping_add(width) as u_int as u_int;
    }
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_next_word_end(
    mut gr: *mut grid_reader,
    mut separators: *const libc::c_char,
) {
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    if (*grid_get_line((*gr).gd, (*gr).cy)).flags & 0x1 as libc::c_int != 0 {
        xx = ((*(*gr).gd).sx).wrapping_sub(1 as libc::c_int as libc::c_uint);
    } else {
        xx = grid_reader_line_length(gr);
    }
    yy = ((*(*gr).gd).hsize)
        .wrapping_add((*(*gr).gd).sy)
        .wrapping_sub(1 as libc::c_int as libc::c_uint);
    while grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0 {
        if grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) != 0 {
            (*gr).cx = ((*gr).cx).wrapping_add(1);
        } else if grid_reader_in_set(gr, separators) != 0 {
            loop {
                (*gr).cx = ((*gr).cx).wrapping_add(1);
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && grid_reader_in_set(gr, separators) != 0
                    && grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) == 0)
                {
                    break;
                }
            }
            return;
        } else {
            loop {
                (*gr).cx = ((*gr).cx).wrapping_add(1);
                if !(grid_reader_handle_wrap(gr, &mut xx, &mut yy) != 0
                    && !(grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) != 0
                        || grid_reader_in_set(gr, separators) != 0))
                {
                    break;
                }
            }
            return;
        }
    }
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_previous_word(
    mut gr: *mut grid_reader,
    mut separators: *const libc::c_char,
    mut already: libc::c_int,
    mut stop_at_eol: libc::c_int,
) {
    let mut oldx: libc::c_int = 0;
    let mut oldy: libc::c_int = 0;
    let mut at_eol: libc::c_int = 0;
    let mut word_is_letters: libc::c_int = 0;
    if already != 0 || grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) != 0 {
        loop {
            if (*gr).cx > 0 as libc::c_int as libc::c_uint {
                (*gr).cx = ((*gr).cx).wrapping_sub(1);
                if grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) != 0 {
                    continue;
                }
                word_is_letters = (grid_reader_in_set(gr, separators) == 0) as libc::c_int;
                break;
            } else {
                if (*gr).cy == 0 as libc::c_int as libc::c_uint {
                    return;
                }
                grid_reader_cursor_up(gr);
                grid_reader_cursor_end_of_line(gr, 0 as libc::c_int, 0 as libc::c_int);
                if !(stop_at_eol != 0 && (*gr).cx > 0 as libc::c_int as libc::c_uint) {
                    continue;
                }
                oldx = (*gr).cx as libc::c_int;
                (*gr).cx = ((*gr).cx).wrapping_sub(1);
                at_eol = grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char);
                (*gr).cx = oldx as u_int;
                if at_eol == 0 {
                    continue;
                }
                word_is_letters = 0 as libc::c_int;
                break;
            }
        }
    } else {
        word_is_letters = (grid_reader_in_set(gr, separators) == 0) as libc::c_int;
    }
    loop {
        oldx = (*gr).cx as libc::c_int;
        oldy = (*gr).cy as libc::c_int;
        if (*gr).cx == 0 as libc::c_int as libc::c_uint {
            if (*gr).cy == 0 as libc::c_int as libc::c_uint
                || !(*grid_get_line(
                    (*gr).gd,
                    ((*gr).cy).wrapping_sub(1 as libc::c_int as libc::c_uint),
                ))
                .flags
                    & 0x1 as libc::c_int
                    != 0
            {
                break;
            }
            grid_reader_cursor_up(gr);
            grid_reader_cursor_end_of_line(gr, 0 as libc::c_int, 1 as libc::c_int);
        }
        if (*gr).cx > 0 as libc::c_int as libc::c_uint {
            (*gr).cx = ((*gr).cx).wrapping_sub(1);
        }
        if !(grid_reader_in_set(gr, b"\t \0" as *const u8 as *const libc::c_char) == 0
            && word_is_letters != grid_reader_in_set(gr, separators))
        {
            break;
        }
    }
    (*gr).cx = oldx as u_int;
    (*gr).cy = oldy as u_int;
}
unsafe extern "C" fn grid_reader_cell_equals_data(
    mut gc: *const grid_cell,
    mut ud: *const utf8_data,
) -> libc::c_int {
    if (*gc).flags as libc::c_int & 0x4 as libc::c_int != 0 {
        return 0 as libc::c_int;
    }
    if (*gc).flags as libc::c_int & 0x80 as libc::c_int != 0
        && (*ud).size as libc::c_int == 1 as libc::c_int
        && *((*ud).data).as_ptr() as libc::c_int == '\t' as i32
    {
        return 1 as libc::c_int;
    }
    if (*gc).data.size as libc::c_int != (*ud).size as libc::c_int {
        return 0 as libc::c_int;
    }
    return (memcmp(
        ((*gc).data.data).as_ptr() as *const libc::c_void,
        ((*ud).data).as_ptr() as *const libc::c_void,
        (*gc).data.size as libc::c_ulong,
    ) == 0 as libc::c_int) as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_jump(
    mut gr: *mut grid_reader,
    mut jc: *const utf8_data,
) -> libc::c_int {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    px = (*gr).cx;
    yy = ((*(*gr).gd).hsize)
        .wrapping_add((*(*gr).gd).sy)
        .wrapping_sub(1 as libc::c_int as libc::c_uint);
    py = (*gr).cy;
    while py <= yy {
        xx = grid_line_length((*gr).gd, py);
        while px < xx {
            grid_get_cell((*gr).gd, px, py, &mut gc);
            if grid_reader_cell_equals_data(&mut gc, jc) != 0 {
                (*gr).cx = px;
                (*gr).cy = py;
                return 1 as libc::c_int;
            }
            px = px.wrapping_add(1);
        }
        if py == yy || (*grid_get_line((*gr).gd, py)).flags & 0x1 as libc::c_int == 0 {
            return 0 as libc::c_int;
        }
        px = 0 as libc::c_int as u_int;
        py = py.wrapping_add(1);
    }
    return 0 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_jump_back(
    mut gr: *mut grid_reader,
    mut jc: *const utf8_data,
) -> libc::c_int {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut xx: u_int = 0;
    xx = ((*gr).cx).wrapping_add(1 as libc::c_int as libc::c_uint);
    py = ((*gr).cy).wrapping_add(1 as libc::c_int as libc::c_uint);
    while py > 0 as libc::c_int as libc::c_uint {
        px = xx;
        while px > 0 as libc::c_int as libc::c_uint {
            grid_get_cell(
                (*gr).gd,
                px.wrapping_sub(1 as libc::c_int as libc::c_uint),
                py.wrapping_sub(1 as libc::c_int as libc::c_uint),
                &mut gc,
            );
            if grid_reader_cell_equals_data(&mut gc, jc) != 0 {
                (*gr).cx = px.wrapping_sub(1 as libc::c_int as libc::c_uint);
                (*gr).cy = py.wrapping_sub(1 as libc::c_int as libc::c_uint);
                return 1 as libc::c_int;
            }
            px = px.wrapping_sub(1);
        }
        if py == 1 as libc::c_int as libc::c_uint
            || (*grid_get_line((*gr).gd, py.wrapping_sub(2 as libc::c_int as libc::c_uint))).flags
                & 0x1 as libc::c_int
                == 0
        {
            return 0 as libc::c_int;
        }
        xx = grid_line_length((*gr).gd, py.wrapping_sub(2 as libc::c_int as libc::c_uint));
        py = py.wrapping_sub(1);
    }
    return 0 as libc::c_int;
}
#[no_mangle]
pub unsafe extern "C" fn grid_reader_cursor_back_to_indentation(mut gr: *mut grid_reader) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    let mut oldx: u_int = 0;
    let mut oldy: u_int = 0;
    yy = ((*(*gr).gd).hsize)
        .wrapping_add((*(*gr).gd).sy)
        .wrapping_sub(1 as libc::c_int as libc::c_uint);
    oldx = (*gr).cx;
    oldy = (*gr).cy;
    grid_reader_cursor_start_of_line(gr, 1 as libc::c_int);
    py = (*gr).cy;
    while py <= yy {
        xx = grid_line_length((*gr).gd, py);
        px = 0 as libc::c_int as u_int;
        while px < xx {
            grid_get_cell((*gr).gd, px, py, &mut gc);
            if (gc.data.size as libc::c_int != 1 as libc::c_int
                || *(gc.data.data).as_mut_ptr() as libc::c_int != ' ' as i32)
                && !(gc.flags as libc::c_int) & 0x80 as libc::c_int != 0
                && !(gc.flags as libc::c_int) & 0x4 as libc::c_int != 0
            {
                (*gr).cx = px;
                (*gr).cy = py;
                return;
            }
            px = px.wrapping_add(1);
        }
        if !(*grid_get_line((*gr).gd, py)).flags & 0x1 as libc::c_int != 0 {
            break;
        }
        py = py.wrapping_add(1);
    }
    (*gr).cx = oldx;
    (*gr).cy = oldy;
}
